package com.fc.advbox.utils.tree;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.util.*;

@JsonSerialize
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class Node {
    @JsonIgnore
    private Integer id;
    private String value;
    private String label;
    @JsonIgnore
    private Integer parentId;

    /**
     * 孩子节点列表
     */
    private List<Node> children = null;

    // 先序遍历，拼接JSON字符串
//    public String toString() {
//        String result = "{"
//                + "name : '" + name + "'"
//                + ", label : '" + label + "'";
//
//        if (children != null && children.getSize() != 0) {
//            result += ", children : " + children.toString();
//        } else {
//            result += "";
//        }
//
//        return result + "}";
//    }

//    // 兄弟节点横向排序
//    public void sortChildren() {
//        if (children != null && children.getSize() != 0) {
//            children.sortChildren();
//        }
//    }

    // 添加孩子节点
    public void addChild(Node node) {
        if(this.children == null){
            this.children = new ArrayList();
        }
        this.children.add(node);
    }

    // 孩子节点排序
    public void sortChildren() {
        if(this.children == null){
            return;
        }
        Collections.sort(children, new NodeIDComparator());
        for (Iterator it = children.iterator(); it.hasNext(); ) {
            ((Node) it.next()).sortChildren();
        }
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    public List<Node> getChildren() {
        return children;
    }

    public void setChildren(List<Node> children) {
        this.children = children;
    }
}
